<template>
	<!-- 子表信息折叠面板 -->
	<view class="collapse">
		<view class="collapse_title" @click="isOpen = !isOpen">
			<view class="collapse_name">
				<span>{{ name }}</span>
				<slot name="label"></slot>
			</view>
			<image src="../../../static/synergy/arrow.png" mode="aspectFill" :style="{transform: isOpen?'rotate(0deg)':'rotate(180deg)'}"></image>
		</view>
		<view :style="{ height: isOpen?'2rpx':'0' }" class="line"></view>
		<view class="collapse_action" :id="elId" :style="{ height: isOpen?mainHeight + 'px':0 }">
			<slot name="handler"></slot>
			<view class="listsmall" v-if="dataSource && dataSource.length > 0" style="display: flex; align-items: center;justify-content: space-between;">
				<view>
					<view class="action" v-for="(item,key) in childFields" :key="item">
						<view class="list1">{{ key }}</view>
						<view class="list2">
							<slot :name="item" :record="dataSource[0]" :text="dataSource[0][item]">{{ dataSource[0][item]}}</slot>
						</view>
					</view>
				</view>
				<view
					style="width: 90rpx; display: flex;justify-content: flex-end; color: #1E6FFF; font-size: 14px;"
					v-if="isShowBtn"
					@click="onClick(dataSource[0], 0)">
					{{ShowBtnText}}
				</view>
			</view>
			<u-empty
				v-else
				mode="data"
				icon="/static/empty/data.png"
			></u-empty>
			<block v-if="dataSource && dataSource.length > 1">
				<view class="child_action" :id="childId" :style="{ height: actionOpen?actionHeight + 'px':0 }">
					<block v-for="(value, index) in dataSource.slice(1)" :key="value.id">
						<view class="listsmall">
							<view class="line" :style="{ height: actionOpen?'2rpx':'0' }"></view>
							<view style="display: flex; align-items: center;justify-content: space-between;">
								<view>
									<view class="action" v-for="(item,key) in childFields" :key="item">
										<view class="list1">{{ key }}</view>
										<view class="list2">
											<slot :name="item" :record="value" :text="value[item]">{{ value[item]}}</slot>
										</view>
									</view>
								</view>
								<view
									style="width: 90rpx; display: flex;justify-content: flex-end; color: #1E6FFF; font-size: 14px;"
									v-if="isShowBtn"
									@click="onClick(value, index+1)">
									{{ShowBtnText}}
								</view>
							</view>
							
						</view>
					</block>
				</view>
				<view class="button" @click="openStatus">{{ actionOpen?'收起':'展开' }}</view>
			</block>
			<slot name="footer"></slot>
		</view>
	</view>
</template>
<script>
	import detailInformationList from './detailInformationList.vue'
	export default {
		data() {
			return {
				isOpen: true,
				actionHeight: '',
				actionOpen: true,
				mainHeight: null,
				elId: uni.$u.guid(36),
				childId: uni.$u.guid(),
			}
		},
		components: {
			detailInformationList
		},
		props: {
			name: {
				type: String
			},
			dataSource: {
				type: Array,
			},
			childFields: {
				type: Object,
				require: true
			},
			// 是否展示按钮
			isShowBtn: {
				type: Boolean,
				default: false
			},
			// 按钮文字
			ShowBtnText: {
				type: String,
				default: '查看'
			},
		},
		watch: {
			dataSource(val){
				this.$nextTick(function() {
					this.setContentMain()
					if(val.length > 1){
						this.setContentChild()
					}
				})
			}
		},
		methods: {
			async setContentMain() {
				// 每次面板打开或者收起时，都查询元素尺寸
				// 好处是，父组件从服务端获取内容后，变更折叠面板后可以获得最新的高度
				const rect = await this.queryRect()
				this.mainHeight = rect.height
			},
			async setContentChild() {
				// 每次面板打开或者收起时，都查询元素尺寸
				// 好处是，父组件从服务端获取内容后，变更折叠面板后可以获得最新的高度
				const rect = await this.queryChildRect()
				this.actionHeight = rect.height
			},
			openStatus(){
				if(this.actionOpen){
					this.mainHeight = this.mainHeight - this.actionHeight
				} else {
					this.mainHeight = this.mainHeight + this.actionHeight
				}
				this.actionOpen = !this.actionOpen
			},
			// 查询内容高度
			queryRect() {
				// #ifndef APP-NVUE
				// $uGetRect为uView自带的节点查询简化方法，详见文档介绍：https://www.uviewui.com/js/getRect.html
				// 组件内部一般用this.$uGetRect，对外的为uni.$u.getRect，二者功能一致，名称不同
				return new Promise(resolve => {
					this.$uGetRect(`#${this.elId}`).then(size => {
						resolve(size)
					})
				})
				// #endif
			},
			// 查询子节点内容高度
			queryChildRect() {
				// #ifndef APP-NVUE
				// $uGetRect为uView自带的节点查询简化方法，详见文档介绍：https://www.uviewui.com/js/getRect.html
				// 组件内部一般用this.$uGetRect，对外的为uni.$u.getRect，二者功能一致，名称不同
				return new Promise(resolve => {
					this.$uGetRect(`#${this.childId}`).then(size => {
						resolve(size)
					})
				})
				// #endif
			},
			// 点击按钮
			onClick (item, index) {
				this.$emit('click', item, index)
			}
		}
	}
</script>
<style lang="scss" scoped>
@import "../common/action.scss";
.child_action{
	transition: height .4s ease-in-out, -webkit-transform .4s ease-in-out, transform .4s ease-in-out;
	transform-origin: 50% 50% 0px;
	overflow: hidden;
}
.button{
	border-top: 2rpx solid rgba(0,0,0,0.1);
	line-height: 40px;
	height: 80rpx;
	text-align: center;
	font-size: 14px;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #1E6FFF;
}
.line{
	width: 100%;
	background: rgba(0,0,0,0.1);
	transition: height .3s;
}
.collapse{
	width: 100%;
	background: #FFFFFF;
	box-shadow: 0px 2px 10px 0px rgba(0,0,0,0.1000);
	border-radius: 10px;
	margin-bottom: 20rpx;
	.collapse_title{
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.collapse_name{
			font-size: 14px;
			// font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #171717;
			line-height: 20px;
		}
		image{
			width: 16px;
			height: 16px;
			transition: transform .3s,-webkit-transform .3s;
		}
	}
	.collapse_action{
		transition: height .4s ease-in-out, -webkit-transform .4s ease-in-out, transform .4s ease-in-out;
		transform-origin: 50% 50% 0px;
		overflow: hidden;
	}
}
</style>